/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;
import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import pojo.Cuahang;
import util.HibernateUtil;
import org.hibernate.*;
import pojo.Hanghoa;
/**
 *
 * @author HoaDinhGiang
 */
public class cuahangDAO {
   
    public static List<Cuahang> timKiemCuaHang(Cuahang ch){
       
        List<Cuahang> ds = null;
         
        int maCuaHang = ch.getMacuahang();
        int maKhuVuc = ch.getKhuvuc().getMakhuvuc();
        String tenCuaHang = ch.getTencuahang();
        String diaChi = ch.getDiachi();
        String dienThoai = ch.getDienthoai();
        String fax = ch.getFax();
        String email = ch.getEmail();
        String website=ch.getWebsite();
        String maSoThue = ch.getMasothue();
        String soTaiKhoan = ch.getSotaikhoan();
        String nganHang = ch.getNganhang();
        
      
      //  hh.getLoaihanghoa().getMaloaihanghoa();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            
            String hql = "select ch from Cuahang ch where (ch.macuahang = "+maCuaHang
                    + "or lower(ch.tencuahang) like lower('"+tenCuaHang+"')"
                    + "or ch.khuvuc.makhuvuc = "+maKhuVuc
                    + "or ch.diachi = '"+diaChi+"'"
                    + "or ch.dienthoai = '"+dienThoai+"'"
                    + "or ch.fax = '"+fax+"'"
                    + "or ch.email = '"+email+"'"
                    + "or ch.website = '"+website+"'"
                    + "or ch.masothue = '"+maSoThue+"'"
                    + "or ch.sotaikhoan = '"+soTaiKhoan+"'"
                    + "or ch.nganhang = '"+nganHang+"'"
                    + ")and ch.kichhoat = true"
                    ;
                   
               //     + "or hh.loaihanghoa.maloaihanghoa = "+maLoai;
        /*    String hql = "select ch from Cuahang ch where ch.macuahang = "+maCuaHang
                    + "or lower(ch.tencuahang) like lower('"+tenCuaHang+"')";
                    * 
                    */
            Query query = session.createQuery(hql);
            ds = query.list();
        }catch(HibernateException ex){
            //Log the exception
            System.err.println(ex);
        }finally{
            session.close();
        }
        return ds;
     
   }

    public static List<Cuahang> layDanhSachCuaHangTonTai(){
        List<Cuahang> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = "select ch from Cuahang ch where ch.kichhoat = true ORDER BY ch.macuahang ASC";
            Query query = session.createQuery(hql);
            ds = query.list();
        }catch(HibernateException ex){
            //Log the exception
            System.err.println(ex);
        }finally{
            session.close();
        }
        return ds;
    }
    public static List<Cuahang> layDanhSachCuaHangDaXoa(){
        List<Cuahang> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = "select ch from Cuahang ch where ch.kichhoat = false ORDER BY ch.macuahang ASC";
            Query query = session.createQuery(hql);
            ds = query.list();
        }catch(HibernateException ex){
            //Log the exception
            System.err.println(ex);
        }finally{
            session.close();
        }
        return ds;
    }
    
    public static Cuahang layThongTinCuaHang(int maCuaHang){
        Cuahang ch = null;
        Session session =  HibernateUtil.getSessionFactory().openSession();
        try{
            ch =  (Cuahang) session.get(Cuahang.class, maCuaHang);
            Hibernate.initialize(ch.getKhuvuc());
        }catch(HibernateException ex){
            //Log the exception
            System.err.println(ex);
        }finally{
            session.close();
        }
        return ch;
    }
 
    public static boolean themCuaHang(Cuahang ch){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction =  session.beginTransaction();
            Serializable save = session.save(ch);
            transaction.commit();
        } catch (HibernateException ex) {
        //Log the exception
            transaction.rollback();
            System.err.println(ex);
            return false;
        } finally {
        session.close();
        }
        return true;
    }
    
    public static boolean capNhatThongTinCuaHang(Cuahang ch) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.update(ch);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
        } finally {
            session.close();
        }
        return true;
}
    public static void xoaTamThoi(Cuahang ch) {
        /*
        Session session =  HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = "update Cuahang ch set kichhoat = false where ch.macuahang = "+maCuaHang;
            System.out.println(hql);
            Query query = session.createQuery(hql);
            int k = query.executeUpdate();
            System.out.print(k);
        }catch(HibernateException ex){
            //Log the exception
            System.err.println(ex);
        }finally{
            session.close();
        }
        * 
        */
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.update(ch);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
        } finally {
            session.close();
        }
}
     public static boolean xoaVinhVien(int maCuaHang) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Cuahang ch = cuahangDAO.layThongTinCuaHang(maCuaHang);
        if(ch==null){
            return false;
        }
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.delete(ch);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
        } finally {
            session.close();
        }
        return true;
}
     
}
